/*navBar解析*/

/**
 * json格式转树状结构
 * @param {json} json数据
 * @param {String} id的字符串
 * @param {String} 父id的字符串
 * @param {String} children的字符串
 * @return {Array} 数组
 */
function transData(a, idStr, pidStr, chindrenStr){
    var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
    for(; i < len; i++){
        hash[a[i][id]] = a[i];
    }
    for(; j < len; j++){
        var aVal = a[j], hashVP = hash[aVal[pid]];
        if(hashVP){
            !hashVP[children] && (hashVP[children] = []);
            hashVP[children].push(aVal);
        }else{
            r.push(aVal);
        }
    }
    return r;
}

function navBar(strData){
    var data;
    if(typeof(strData) == "string"){
        var data = JSON.parse(strData); //部分用户解析出来的是字符串，转换一下
    }else{
        data = strData;
    }
    var ulHtml = '<ul class="layui-nav layui-nav-tree">';
    for(var i=0;i<data.length;i++){
        if(data[i].spread){
            ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
        }else{
            ulHtml += '<li class="layui-nav-item">';
        }
        if(data[i].children != undefined && data[i].children.length > 0){
            ulHtml += '<a href="javascript:;">';
            if(data[i].icon != undefined && data[i].icon != ''){
                if(data[i].icon.indexOf("fa-") != -1){
                    ulHtml += '<i class="fa '+data[i].icon+'" data-icon="'+data[i].icon+'" aria-hidden="true"></i>';
                }else{
                    ulHtml += '<i class="layui-icon" data-icon="'+data[i].icon+'">'+data[i].icon+'</i>';
                }
            }
            ulHtml += '<cite>'+data[i].title+'</cite>';
            ulHtml += '<span class="layui-nav-more"></span>';
            ulHtml += '</a>';
            ulHtml += '<dl class="layui-nav-child">';
            for(var j=0;j<data[i].children.length;j++){
                if(data[i].children[j].target == "_blank"){
                    ulHtml += '<dd><a href="javascript:;" data-url="'+data[i].children[j].href+'" target="'+data[i].children[j].target+'">';
                }else{
                    ulHtml += '<dd><a href="javascript:;" data-url="'+data[i].children[j].href+'">';
                }
                if(data[i].children[j].icon != undefined && data[i].children[j].icon != ''){
                    if(data[i].children[j].icon.indexOf("fa-") != -1){
                        ulHtml += '<i class="fa '+data[i].children[j].icon+'" data-icon="'+data[i].children[j].icon+'" aria-hidden="true" "></i>';
                    }else{
                        ulHtml += '<i class="layui-icon" data-icon="'+data[i].children[j].icon+'">'+data[i].children[j].icon+'</i>';
                    }
                }
                ulHtml += '<cite>'+data[i].children[j].title+'</cite></a></dd>';
            }
            ulHtml += "</dl>";
        }else{
            if(data[i].target == "_blank"){
                ulHtml += '<a href="javascript:;" data-url="'+data[i].href+'" target="'+data[i].target+'">';
            }else{
                ulHtml += '<a href="javascript:;" data-url="'+data[i].href+'">';
            }
            if(data[i].icon != undefined && data[i].icon != ''){
                if(data[i].icon.indexOf("fa-") != -1){
                    ulHtml += '<i class="fa '+data[i].icon+'" aria-hidden="true" data-icon="'+data[i].icon+'"></i>';
                }else{
                    ulHtml += '<i class="layui-icon" data-icon="'+data[i].icon+'"></i>';
                }
            }
            ulHtml += '<cite>'+data[i].title+'</cite></a>';
        }
        ulHtml += '</li>';
    }
    ulHtml += '</ul>';
    return ulHtml;
}
